import { Button, message, Form, Input } from 'antd';
import React from 'react';
import request from '../../utils/request';
import { useNavigate } from 'react-router-dom';

function Useradd() {
    const navigate = useNavigate()
    const onFinish = async(values) => {
        const data2 = await request.post('/user/check',values)
        if (data2.data.code === 200){
            const data = request.post('/user/reg',values)
            message.success('用户添加成功')
            navigate('/manage/user_list')
        }else{
            message.success('用户已存在')
        }
      };

      return (
        <div className='useradd'>
            <Form
            name="basic"
            labelCol={{
                span: 8,
            }}
            wrapperCol={{
                span: 16,
            }}
            initialValues={{
                remember: true,
            }}
            onFinish={onFinish}
            autoComplete="off"
            >
            <Form.Item
                wrapperCol={{
                offset: 8,
                span: 16,
                }}
            >
                <h1 className='useradd_head'>添加用户</h1>
            </Form.Item>
            <Form.Item
                label="用户名"
                name="username"
                rules={[
                {
                    required: true,
                    message: '用户名不能为空',
                },
                ]}
            >
                <Input />
            </Form.Item>
            <Form.Item
                label="密码"
                name="password"
                rules={[
                {
                    required: true,
                    message: '密码不能为空',
                },
                ]}
            >
                <Input.Password />
            </Form.Item>
        
            <Form.Item
                wrapperCol={{
                offset: 14,
                span: 16,
                }}
            >
                <Button type="primary" htmlType="submit">
                提交
                </Button>
            </Form.Item>
            <Form.Item
                wrapperCol={{
                offset: 11,
                span: 16,
                }}
            >
                <span>提示：请填写用户名与登录密码信息</span>
            </Form.Item>
            </Form>
        </div>
      );
}

export default Useradd